<template>
	<div class="container">
		<section class="data">
			<!-- v-for -->
			<div @click="getDetail" data-code="wait">
				<h2 data-code="wait">待执行任务数</h2>
				<h3 class="underline" data-code="wait">
					{{ static_data.waiting_cnt }}
				</h3>
			</div>
			<div
				data-code="fail"
				@click="getDetail"
				v-if="static_data.failed_cnt > 0"
			>
				<h2 data-code="fail">执行任务失败数</h2>
				<h3 class="underline" data-code="fail">{{ static_data.failed_cnt }}</h3>
			</div>
			<div
				:key="index"
				v-for="(item, index) in static_data.depart_list"
				@click="getDetail"
				:data-code="item.depart_code"
			>
				<h2 :data-code="item.depart_code">{{ item.depart_name }}</h2>
				<h3 class="underline" :data-code="item.depart_code">
					{{ item.count }}
				</h3>
			</div>
		</section>
		<!-- 搜索 -->
		<search-Bar
			:searchBarData="searchBarData"
			:taskData="mulitIds"
			@refreshSearch="getRefreshsearch"
		></search-Bar>
		<my-table
			:col-configs="colConfigs"
			:list-loading="listLoading"
			:page-size="listQuery.pagesize"
			:table-data="tableData"
			:total="total"
			@handleMulSelection="handleMulSelection"
			@refreshList="getList"
			class="table"
		>
			<el-table-column label="任务类型" slot="task_type" width="150px">
				<template slot-scope="{ row }">
					<!--  1编辑 2 删除 -->
					<span v-if="row.task_type === 0">图文报告采集</span>
					<span v-if="row.task_type === 1">影像下载</span>
					<span v-if="row.task_type === 2">影像上传</span>
					<span v-if="row.task_type === 3">胶片采集</span>
				</template>
			</el-table-column>
			<el-table-column label="任务状态" slot="task_status" width="120px">
				<template slot-scope="{ row }">
					<!--  1编辑 2 删除 -->
					<span v-if="row.task_status === -1">执行失败</span>
					<span v-if="row.task_status === 0">待执行</span>
					<span v-if="row.task_status === 1">执行中</span>
					<span v-if="row.task_status === 2">执行成功</span>
				</template>
			</el-table-column>
		</my-table>
	</div>
</template>

<script>
import SearchTime from '@/components/common/SearchTime'
import searchDate from '@/components/common/searchDate'
import searchBar from './search.vue'
import myTable from '@/components/myTable'
import {
	getTaskStatistic,
	creaTask,
	deleteTask,
	updateTask,
	taskRetry,
	taskPriority,
	getTaskStatic
} from '@/api/task-mg'
import { clone } from '@/utils/base.js'
import { mapGetters } from 'vuex'

export default {
	name: 'taskManager',
	components: {
		SearchTime,
		myTable,
		searchBar
	},
	data () {
		return {
			colConfigs: [
				{
					type: 'selection',
					width: '50px'
				},
				{
					type: 'index',
					label: '序号',
					width: '50px'
				},
				{ slot: 'task_type' },
				{ slot: 'task_status' },
				{
					prop: 'patient_name',
					label: '患者姓名'
				},
				{
					prop: 'patient_id',
					label: '患者编号',
					width: '150px'
				},
				{
					prop: 'depart_name',
					label: '检查科室',
					width: '120px'
				},
				{
					prop: 'accession_number',
					label: '检查号',
					width: '150px'
				},
				{
					prop: 'modilty',
					label: '检查类型',
					width: '120px'
				},
				{
					prop: 'perform_time',
					label: '检查时间',
					width: '180px'
				},
				{
					prop: 'create_time',
					label: '任务创建时间',
					width: '180px'
				},
				{
					prop: 'process_time',
					label: '任务执行时间',
					width: '180px'
				},
        /* 	{
						prop: 'end_time',
						label: '任务完成时间',
						width: '180px'
					}, */
				{
					prop: 'error_info',
					label: '错误信息',
					width: '200px'
				}
			],
			listQuery: {
				pagesize: 10,
				pageindex: 1,
				time_type: null,
				task_status: null,
				depart_code: null
			},
			tableData: [],
			total: null,
			listLoading: false,
			mulitIds: [], //  多选ID
			static_data: {
				waiting_cnt: 0,
				failed_cnt: 0,
				depart_list: []
			},
			searchBarData: {
				timeTypeList: [
					// { label: '不限', value: 0 },
					{ label: '检查时间', value: 1 },
					// { label: '归档时间', value: 4 },
					{ label: '创建时间', value: 5 }
				],
				customQueryList: [
					{ label: '患者姓名', value: 0 },
					{ label: '病历号', value: 1 },
					{ label: '检查号', value: 2 },
					{ label: '身份证号', value: 3 },
					{ label: '患者编号', value: 4 },
					{ label: '门诊号', value: 5 }
				]
			},
			taskList: {
				taskStatusList: [
					{ task_status: null, label: '不限' },
					{ task_status: 0, label: '待执行' },
					{ task_status: 1, label: '执行中' },
					{ task_status: -1, label: '执行失败' },
					{ task_status: 3, label: '执行成功' }
				],
				taskTypeList: [
					{ task_status: null, label: '不限' },
					{ task_status: 0, label: '图文报告采集' },
					{ task_status: 1, label: '影像下载' },
					{ task_status: 2, label: '影像上传' },
					{ task_status: 3, label: '胶片采集' }
				]
			}
		}
	},
	computed: {
	},
	created () {
	},

	mounted () {
		this.getList()
		this.getTaskStatic()
	},
	methods: {
		getList ( val ) {
			// 获取任务列表
			let params = Object.assign( this.listQuery, val )
			getTaskStatistic( params ).then( res => {
				if ( res.code === 0 ) {
					this.tableData = res.data
					this.total = res.page.total_count
				}
			} )
		},
		getRefreshsearch ( val ) {
			// 多个查询条件
			console.log( val )
			let obj = {
				custom_query_type: val.custom_query_type,
				custom_query_value: val.custom_query_value,
				time_type: val.time_type,
				start_time: val.start_time,
				end_time: val.end_time
			}
			this.listQuery = Object.assign( this.listQuery, obj )
			console.log( this.listQuery )
			// 判断是否是重置
			if ( val.hasOwnProperty( 'pageindex' ) ) {
				// 重置
				this.listQuery = {}
				this.listQuery = Object.assign( {}, val )
			}
			this.getList()

		},
		getTaskStatic () {
			// 统计信息获取
			let params = { org_code: this.$store.state.app.org_code }
			getTaskStatic( params ).then( res => {
				if ( res.code === 0 ) {
					this.static_data = res.data
				}
			} )
		},
		handleMulSelection ( row ) {
			// 多选
			this.mulitIds = row.map( item => item.id )
			console.log( this.mulitIds )
		},
		getDetail ( $event ) {
			// 查询详情
			let depart_code = $event.target.getAttribute( 'data-code' )
			console.log( depart_code );
			this.listQuery = Object.assign( {}, { pageindex: 1, pagesize: 10 } )
			if ( depart_code === 'wait' ) {
				// 待执行
				this.listQuery.task_status = 0
			} else if ( depart_code === 'fail' ) {
				// 执行失败
				this.listQuery.task_status = '-1'
			} else {
				// 科室
				this.listQuery.depart_code = depart_code
			}
			this.getList()

		}
	}
}
</script>

<style scoped lang="less">
.container {
	.table {
		margin-top: 30px;
	}
	padding: 0 10px;
	position: relative;
	.data {
		// height: 161px;
		padding: 23px 0;
		box-shadow: 0px 0px 14px 0px rgba(1, 1, 100, 0.1);
		border-radius: 8px;
		margin-bottom: 40px;
		// text-align: center;
		div {
			height: 75px;
			width: 12%;
			display: inline-block;
			border-right: 1px solid #ddd;
			text-align: center;
			h2 {
				font-size: 16px;
				font-weight: normal;
				height: 42px;
				line-height: 22px;
				color: #333333;
			}
			h3 {
				height: 42px;
				font-size: 30px;
				font-family: PingFangSC-Medium, PingFang SC;
				font-weight: 500;
				line-height: 42px;
				color: @49B523;
			}
		}
		> :last-child {
			border: none !important;
		}
		> :last-child,
		> :nth-child(5) {
			h3 {
				color: @FF6F6F;
			}
		}
		> :nth-child(6) {
			h3 {
				color: @333;
			}
		}
	}
}
</style>
